let requestUrl;

function addHandler() {
	let schoolId = $("#school_id").combobox("getValue");

	if(schoolId) {
		$("#schoolId").combobox("setValue", schoolId);
	}

	requestUrl = "/school_skill_category/insert";

	$("#skill_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#skill_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/school_skill_category/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#note").textbox("setValue", rowData.note);
		$("#improveEffect").textbox("setValue", rowData.improveEffect);
		$("#efficacy").textbox("setValue", rowData.efficacy);
		$("#schoolId").combobox("setValue", rowData.schoolId);
		$("#propertyId").combobox("setValue", rowData.propertyId);
		$("#propertyValue").numberspinner("setValue", rowData.propertyValue);

		$("#skill_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function imageHandler() {
	let rowData = $("#skill_list").datagrid("getSelected");

	if (rowData) {
		$("#skill_id").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

$(document).ready(function() {

	$("#school_id").combobox({
		url: "/school/selectAll",
        valueField: "id",
		textField: "name",
		width: 150,
		prompt: "-选择门派-",
		panelHeight: "auto",
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});
	
	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let schoolId = $("#school_id").combobox("getValue");
		
		$("#skill_list").datagrid("reload", {
			schoolId: schoolId
		});
	});
	
	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#name").textbox({
		width: 150,
		required: true
	});
	
	fetchSchoolId();

	$("#propertyId").combobox({
		url: "/property/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: false,
		panelHeight: "auto"
	});

	$("#propertyValue").numberspinner({
		min: 0.5,
		width: 150,
		precision: 1,
		increment: 0.5
	});
	
	/**
	 * 简介
	 */
	$("#note").textbox({
		width: 380,
    	height: 50,
		required: true,
		multiline: true
	});

	/**
	 * 功效
	 */
	$("#efficacy").textbox({
		width: 380,
    	height: 50,
		required: true,
		multiline: true
	});

	/**
	 * 升级效果
	 */
	$("#improveEffect").textbox({
		width: 380,
    	height: 50,
    	required: true,
		multiline: true
	});

	/**
	 * 门派技能对话框
	 */
	$("#skill_dialog").dialog({
		title: "门派技能",
		width: 500,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#skill_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#skill_dialog").dialog("close");
						$("#skill_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#skill_dialog").dialog("close");
				$("#skill_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#skill_list", "/school_skill_category/upload");

	fetchUploadDialog("/school_skill_category/updateImageById", function () {
		$("#skill_list").datagrid("reload");
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#skill_list").datagrid({
		url: "/school_skill_category/selectByPage",
		method: "get",
		height: height_table,
		rownumbers: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", imageUpdateButton],
		frozenColumns: [[
			{field: "id", hidden: true},
			{field: "name", title: "技能名称", align: "center", width: 120},
			{field: "image", title: "技能图标", align: "center", width: 120
				, formatter: function(value) {
					return "<img height='" + size + "' src='" + value + "' />";
				}
			},
		]],
		columns: [[
			{field: "propertyId", title: "增加属性", align: "center", width: 120
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/property/selectById", {
							id: value
						}, function (resp) {
							let data = resp.data;

							$("#property_" + rowIndex).html(data.name);
						}, error);

						return "<div class='blue' id='property_" + rowIndex + "' ></div>";
					}

					return "/";
				}
			},
			{field: "propertyValue", title: "属性值", align: "center", width: 120
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						return value;
					}

					return "/";
				}
			},
			{field: "note", title: "技能简介", align: "center", width: 250
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			},
			{field: "efficacy", title: "技能效果", align: "center", width: 250
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			},
			{field: "improveEffect", title: "升级效果", align: "center", width: 250
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			},
			{field: "schoolId", title: "所属门派", align: "center", width: 120
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/school/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						let content = "<span class='white'>" + data.name + "：" + data.note + "</span>";
						
						$("#school_" + rowIndex).attr("src", data.image).tooltip({
					        position: "bottom",
					        content: content,
					        onShow: function() {
					    		$(this).tooltip("tip").css({
					    			backgroundColor: "#666",
					    			borderColor: "#666"
					    		});
					        }
					    });
					});
					
					return "<img height='" + size + "' id='school_" + rowIndex + "' />";
				}
			}
		]]
	});

});